package logic;

public class Vec2D {
	protected float x = 0.0f, y = 0.0f;
	
	public Vec2D(float x, float y)
	{
		this.x = x;
		this.y = y;
	}
	
	public Vec2D(Vec2D vec)
	{
		x = vec.x;
		y = vec.y;
	}
	
	public float getLength()
	{
		return (float) Math.sqrt(x * x + y * y);
	}
	
	public void normalize()
	{
		float l = getLength();
		if(l == 0.0f) return;
		x /= l;
		y /= l;
	}
	
	public Vec2D getNormalized()
	{
		Vec2D n = new Vec2D(this);
		n.normalize();
		return n;
	}
	
	public Vec2D getInverse()
	{
		Vec2D v = new Vec2D(this);
		v.x *= -1.0f;
		v.y *= -1.0f;
		return v;
	}
	
	protected Vec2D _add(Vec2D vec)
	{
		x += vec.x;
		y += vec.y;
		return this;
	}
	
	protected Vec2D _mul(float scalar)
	{
		x *= scalar;
		y *= scalar;
		return this;
	}
	
	protected Vec2D _set(float x, float y)
	{
		this.x = x;
		this.y = y;
		return this;
	}
	
	public Vec2D add(Vec2D vec)
	{
		return new Vec2D(this)._add(vec);
	}
	
	public Vec2D sub(Vec2D vec)
	{
		return new Vec2D(this)._add(vec.getInverse());
	}
	
	public Vec2D mul(float scalar)
	{
		return new Vec2D(this)._mul(scalar);
	}
	
	public Vec2D div(float scalar)
	{
		if(scalar == 0.0f) return this;
		return new Vec2D(this)._mul(1.0f / scalar);
	}
	
	public float dot(Vec2D vec)
	{
		return x * vec.x + y * vec.y;
	}
	
	public Vec2D reflect(Vec2D normal)
	{
		normal = new Vec2D(normal.y, normal.x);
		return normal.mul((2.0f * this.dot(normal))).sub(this);
	}
	
	public float distanceTo(Vec2D point)
	{
		return this.sub(point).getLength();
	}
	
	public String toString()
	{
		return "[V2D : " + x + ", " + y + "]";
	}
	
	public float x()
	{
		return x;
	}
	
	public float y()
	{
		return y;
	}

	public Vec2D add(float f, float g) {
		return new Vec2D(x + f, y + g);
	}
}
